﻿'Copyright (C) 2010 Matthew Thompson (matt_b_thompson@hotmail.com - http://mcservertools.codeplex.com)

'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 2
'of the License, or (at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Imports System.IO
Imports System.Xml

Public Class documentParser



#Region "File/Folder Creation"

    Public Sub folderCreation(ByVal folderName)


        If Not Directory.Exists(folderName) Then
            Directory.CreateDirectory(folderName)
        End If

    End Sub

    Public Sub fileCreation(ByVal fileName)

        If Not File.Exists(fileName) Then
            File.Create(fileName)
            Exit Sub

        End If

    End Sub

#End Region


#Region "XML Parsing"


    Public Sub xmlWriteConfig(ByVal fileName)

        Dim MemoryStream As New MemoryStream(1024)
        Dim xmlTextWriter As New XmlTextWriter(MemoryStream, System.Text.Encoding.UTF8)
        Dim CurFileName As String = fileName
        Dim boolWriterClosed As Boolean = True



        If Not File.Exists(CurFileName) Then

            xmlTextWriter = New XmlTextWriter(CurFileName, Nothing)

        Else
            xmlTextWriter = New XmlTextWriter(CurFileName, System.Text.Encoding.UTF8)
        End If



        boolWriterClosed = False

        xmlTextWriter.Formatting = Formatting.Indented
        xmlTextWriter.Indentation = 4

        'Write xml declaration
        xmlTextWriter.WriteStartDocument(True)

        xmlTextWriter.WriteStartElement("mcServerTools")

        xmlTextWriter.WriteStartElement("mcServerToolsSettings")

        'MineCraft Settings
        xmlTextWriter.WriteStartElement("mcServerSettings")

        xmlTextWriter.WriteElementString("mcServerDir", mcServerDir)
        xmlTextWriter.WriteElementString("mcServerLocalVer", mcServerLocalVer)
        xmlTextWriter.WriteElementString("mcServerRemoteVer", mcServerRemoteVer)
        xmlTextWriter.WriteElementString("mcServerUseWhiteList", mcUseWhiteList)
        xmlTextWriter.WriteElementString("mcServerAutoDownloadUpdates", mcAutoDownloadNew)

        xmlTextWriter.WriteEndElement()

        'CraftBukkit Settings
        xmlTextWriter.WriteStartElement("cbSettings")

        xmlTextWriter.WriteElementString("cbLocalVer", cbLocalVer)
        xmlTextWriter.WriteElementString("cbRemoteVer", cbRemoteVer)
        xmlTextWriter.WriteElementString("cbAutoDownloadUpdates", cbAutoDownloadNew)
        xmlTextWriter.WriteElementString("cbUseStable", cbUseStable)

        xmlTextWriter.WriteEndElement()


        'Plugin Settings
        xmlTextWriter.WriteStartElement("pluginSettings")

        xmlTextWriter.WriteElementString("pluginAutoDownloadUpdates", pluginAutoDownloadable)

        xmlTextWriter.WriteEndElement()
        xmlTextWriter.WriteEndElement()
        xmlTextWriter.WriteEndElement()



        xmlTextWriter.Close()


    End Sub

    Public Sub xmlReadConfig(ByVal fileName)




        Try
            Dim tempBool As String
            Dim xmlRead As XmlDocument
            Dim xml_nl As XmlNodeList
            Dim xml_node As XmlNode
            'Create the XML Document

            xmlRead = New XmlDocument()
            'Load the Xml file

            xmlRead.Load(fileName)
            'Get the list of name nodes 

            'MineCraft Server Settings
            xml_nl = xmlRead.SelectNodes("mcServerTools/mcServerToolsSettings/mcServerSettings")
            'Loop through the nodes

            For Each xml_node In xml_nl



                mcServerDir = xml_node.ChildNodes.Item(0).InnerText


                mcServerLocalVer = xml_node.ChildNodes.Item(1).InnerText


                mcServerRemoteVer = xml_node.ChildNodes.Item(2).InnerText



                tempBool = xml_node.ChildNodes.Item(3).InnerText
                If tempBool.ToLower = "true" Then
                    mcUseWhiteList = True
                Else
                    mcUseWhiteList = False
                End If


                tempBool = xml_node.ChildNodes.Item(4).InnerText
                If tempBool.ToLower = "true" Then
                    mcAutoDownloadNew = True
                Else
                    mcAutoDownloadNew = False
                End If


            Next

            'CraftBukkit Settings
            xml_nl = xmlRead.SelectNodes("mcServerTools/mcServerToolsSettings/cbSettings")


            For Each xml_node In xml_nl

                cbLocalVer = xml_node.ChildNodes.Item(0).InnerText
                cbRemoteVer = xml_node.ChildNodes.Item(1).InnerText

                tempBool = xml_node.ChildNodes.Item(2).InnerText
                If tempBool = "true" Then
                    cbAutoDownloadNew = True
                Else
                    cbAutoDownloadNew = False
                End If

                tempBool = xml_node.ChildNodes.Item(3).InnerText
                If tempBool.ToLower = "true" Then
                    cbUseStable = True
                Else
                    cbUseStable = False
                End If

            Next

            'Plugin Settings
            xml_nl = xmlRead.SelectNodes("mcServerTools/mcServerToolsSettings/pluginSettings")


            For Each xml_node In xml_nl

                tempBool = xml_node.ChildNodes.Item(0).InnerText
                If tempBool = "true" Then
                    pluginAutoDownloadNew = True
                Else
                    pluginAutoDownloadNew = False
                End If

            Next


        Catch errorVariable As Exception
            'Error trapping

            Console.Write(errorVariable.ToString())
        End Try


    End Sub


#End Region

End Class
